package com.bootdo.app.interceptor;

import com.bootdo.app.exception.AppAjaxException;
import com.bootdo.app.exception.AppException;
import com.bootdo.app.exception.RCode;
import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.common.redis.shiro.RedisCache;
import com.bootdo.common.redis.shiro.RedisManager;
import com.bootdo.common.utils.HttpContextUtils;
import com.bootdo.common.utils.HttpServletUtils;
import com.bootdo.common.utils.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppSecurityInterceptor implements HandlerInterceptor {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        Object obj = request.getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
		if (obj == null || !(obj instanceof UseraccountDO)) {
			if(HttpServletUtils.jsAjax(request)) {
				throw new AppException(RCode.NEED_LOGIN);
			}
			response.sendRedirect(request.getContextPath() + "/applogin");
			return false;
		}
        return true;
	}

	
}
